<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/7
 * Time: 23:33
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\Coupon;
use common\models\DAO\Hongbao;
use common\models\DAO\UserCoupon;
use common\models\DAO\UserHongbao;
use common\models\DAO\UserHongbaoList;
use common\msg\UserMsg;

class ConvertService {
    public static function getCoupon($id = 0){
        $now = time();
        if($id != 0){
            $result['status'] = 0;
            $coupon = Coupon::findOne($id);
            if($coupon['createTime'] > $now){
                $result['msg'] = UserMsg::$action_no;
                return $result;
            }
            if($coupon['stock'] == 0){
                $result['msg'] = UserMsg::$coupon_stock;
                return $result;
            }
            if( $coupon['endTime'] < $now){
                $result['msg'] = UserMsg::$coupon_lose;
                return $result;
            }

            return $coupon;
        }
        $list = Coupon::find()->where('endTime >'.$now)->asArray()->all();
        $coupon = [];
        $rec_coupon = [];
        foreach($list as $v){
            $v['card'] = CommonFunction::formatMoney($v['card']);
            $v['date'] = date('m月d日 H:i', $v['createTime']);
            $v['status'] = 1;
            if($v['stock'] == 0){
                $v['status'] = 0;
            }

            if($v['createTime'] > $now){
                $v['status'] = 2;
            }

            if($v['isRec'] == 1)
                $rec_coupon[] = $v;
            else
                $coupon[] = $v;
        }
        return ['coupon'=>$coupon, 'rec_coupon'=>$rec_coupon];
    }
    public static function getHongbao($id = 0){
        $now = time();
        if($id != 0){
            $hb = Hongbao::findOne($id);
            if($hb['createTime'] > $now){
                $result['msg'] = UserMsg::$action_no;
                return $result;
            }
            if($hb['stock'] == 0){
                $result['msg'] = UserMsg::$hongbao_stock;
                return $result;
            }
            if($hb['endTime'] < $now){
                $result['msg'] = UserMsg::$hongbao_lose;
                return $result;
            }
            return $hb;
        }
        $list = Hongbao::find()->where('endTime >'.$now)->asArray()->all();
        $hongbao = [];
        $rec_hongbao = [];
        foreach($list as $v){
            $v['hongBao'] = CommonFunction::formatMoney($v['hongBao']);
            $v['date'] = date('m月d日 H:i', $v['createTime']);
            $v['status'] = 1;
            if($v['stock'] == 0){
                $v['status'] = 0;
            }

            if($v['createTime'] > $now){
                $v['status'] = 2;
            }
            if($v['isRec'] == 1)
                $rec_hongbao[] = $v;
            else
                $hongbao[] = $v;
        }
        return ['hongbao'=>$hongbao, 'rec_hongbao'=>$rec_hongbao];
    }

    public static function get($data){
        $result['status'] = 0;
        $result['msg'] = UserMsg::$time_out;
        $typeID = $data['typeID'];
        $userID = $data['userID'];
        $id = intval($data['id']);
        $now = time();
        $param['road'] = 1;
        $transaction = \Yii::$app->db->beginTransaction();
        $param['couponID'] =  0;
        $param['hongBaoID'] =  0;
        $param['lotteryID'] =  0;
        if($typeID == 1){
            $data = self::getHongbao($id);
            if(!$data){
                $result['msg'] = UserMsg::$hongbao_lose;
                return $result;
            }

            $user_hongbao = UserHongbao::findOne($userID);
            if($user_hongbao){
                $user_hongbao->money = $user_hongbao['money'] + $data['hongBao'];
                $user_hongbao->updateTime = $now;
            }else{
                $user_hongbao = new UserHongbao();
                $user_hongbao->money = $data['hongBao'];
                $user_hongbao->updateTime = $now;
                $user_hongbao->userID = $userID;
            }
            $hongbao_result = $user_hongbao->save();
            if(!$hongbao_result){
                $transaction->rollBack();
                return $result;
            }
            $user_hongbao_list = new UserHongbaoList();
            $user_hongbao_list->userID = $userID;
            $user_hongbao_list->money = $data['hongBao'];
            $user_hongbao_list->createTime = $now;
            $user_hongbao_list->haoBaoID = $data['id'];
            $user_hongbao_list_result = $user_hongbao_list->save();
            if(!$user_hongbao_list_result){
                $transaction->rollBack();
                return $result;
            }
            $param['hongBaoID'] = $user_hongbao_list->id;
            $param['desc'] = '领取'.CommonFunction::formatMoney($data['hongBao']).'红包';
        }else {
            $data = self::getCoupon($id);
            if(isset($data['status'])){
                return $data;
            }
            $user_coupon = new UserCoupon();
            $user_coupon->userID = $userID;
            $user_coupon->createTime = $now;
            $user_coupon->card = $data['card'];
            $lose_day = intval($data['loseDay']) + 1;
            $user_coupon->loseTime = date('Ymd', strtotime("+$lose_day day"));
            $user_coupon->couponID = $data['id'];
            $user_coupon_result = $user_coupon->save();
            if(!$user_coupon_result){
                $transaction->rollBack();
                return $result;
            }

            $param['couponID'] = $user_coupon->id;
            $param['desc'] = '领取'.CommonFunction::formatMoney($data['card']).'%加息券';
        }

        $userIntegral = UserIntegral::get($userID);
        if($userIntegral < $data['integral']){
            $transaction->rollBack();
            $result['msg'] = UserMsg::$no_integral;
            return $result;
        }
        //更新库存
        $data->stock = $data['stock'] - 1;
        $result_data = $data->save();
        if(!$result_data){
            $transaction->rollBack();
            return $result;
        }
        // $lotteryID = $param['lotteryID'];
        $param['userID'] = $userID;
        $param['integral'] = $data['integral'];
        $param['createTime'] = $now;
        $integral_deduct = UserIntegral::deduct($param);
        if($integral_deduct){
            $transaction->commit();
            return ['status'=>1];
        }else{
            $transaction->rollBack();
            return $result;
        }
    }
}